<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <include file="./Public/Tpl/head.html" title="销售管理" description="药品管理系统." keywords="中南民族大学,药品管理系统"/>
</head>
<body>
<include file="./Public/Tpl/nav.html"/>
<div class="container">
  <div class="col-md-2">
    <div class="panel panel-default">
      <div class="panel-heading">功能</div>
      <div class="list-group list-menu">
        <button type="button" class="list-group-item list-menu active" data-to="0">销售药品</button>
        <button type="button" class="list-group-item list-menu" data-to="1" onclick="loadList(size,page)">销售记录</button>
      </div>
    </div>
  </div>
  <div class="col-sm-10">
    <div class="panel panel-default func">
      <div class="panel-heading" style="text-align: center">销售</div>
      <div class="panel-body text-info">
        <form id="sellForm" class="form-horizontal">
          <div class="form-group">
            <label for="buyyer" class="col-sm-2 control-label">购方单位:</label>
            <div class="col-sm-10" data-toggle="tooltip" data-placement="top" title="发票抬头">
              <input name="buyyer" type="text" class="form-control" id="buyyer" placeholder="">
            </div>
          </div>
          <table class="table table-bordered">
            <thead>
            <th style="width: 15%;">拼音码</th>
            <th style="width: 22%;">名称</th>
            <th style="width: 15%">规格大小</th>
            <th style="width: 8%;">计量单位</th>
            <th style="width: 10%;">单价</th>
            <th style="width: 10%;">数量</th>
            <th style="width: 10%;">小计</th>
            </thead>
            <tbody>
            <tr>
              <td>
                <div class="" data-toggle="tooltip" data-placement="top">
                  <input name="pinyinma[]" type="text" class="form-control" placeholder="拼音码" autocomplete="off">
                  <input name="stock_id[]" type="hidden" class="form-control">
                </div>
              </td>
              <td>
                <div class="">
                  <input name="name[]" type="text" class="form-control" readonly placeholder="药品名称" tabindex="-1">
                </div>
              </td>
              <td>
                <input name="spec[]" type="text" class="form-control" readonly placeholder="规格" tabindex="-1">
              </td>
              <td>
                <input name="unit[]" type="text" class="form-control" readonly placeholder="单位" tabindex="-1">
              </td>
              <td>
                <input name="price[]" type="text" class="form-control" readonly placeholder="0.00" tabindex="-1">
              </td>
              <td>
                <input name="amount[]" type="number" class="form-control" placeholder="0">
              </td>
              <td>
                <input name="subtotal[]" type="number" class="form-control" readonly placeholder="自动计算..." tabindex="-1">
              </td>
            </tr>
            <tr>
              <td>
                <div class="" data-toggle="tooltip" data-placement="top">
                  <input name="pinyinma[]" type="text" class="form-control" placeholder="拼音码" autocomplete="off">
                  <input name="stock_id[]" type="hidden" class="form-control">
                </div>
              </td>
              <td>
                <div class="">
                  <input name="name[]" type="text" class="form-control" readonly placeholder="药品名称" tabindex="-1">
                </div>
              </td>
              <td>
                <input name="spec[]" type="text" class="form-control" readonly placeholder="规格" tabindex="-1">
              </td>
              <td>
                <input name="unit[]" type="text" class="form-control" readonly placeholder="单位" tabindex="-1">
              </td>
              <td>
                <input name="price[]" type="text" class="form-control" readonly placeholder="0.00" tabindex="-1">
              </td>
              <td>
                <input name="amount[]" type="number" class="form-control" placeholder="0">
              </td>
              <td>
                <input name="subtotal[]" type="number" class="form-control" readonly placeholder="自动计算..." tabindex="-1">
              </td>
            </tr>
            <tr>
              <td>
                <div class="" data-toggle="tooltip" data-placement="top">
                  <input name="pinyinma[]" type="text" class="form-control" placeholder="拼音码" autocomplete="off">
                  <input name="stock_id[]" type="hidden" class="form-control">
                </div>
              </td>
              <td>
                <div class="">
                  <input name="name[]" type="text" class="form-control" readonly placeholder="药品名称" tabindex="-1">
                </div>
              </td>
              <td>
                <input name="spec[]" type="text" class="form-control" readonly placeholder="规格" tabindex="-1">
              </td>
              <td>
                <input name="unit[]" type="text" class="form-control" readonly placeholder="单位" tabindex="-1">
              </td>
              <td>
                <input name="price[]" type="text" class="form-control" readonly placeholder="0.00" tabindex="-1">
              </td>
              <td>
                <input name="amount[]" type="number" class="form-control" placeholder="0">
              </td>
              <td>
                <input name="subtotal[]" type="number" class="form-control" readonly placeholder="自动计算..." tabindex="-1">
              </td>
            </tr>
            <tr>
              <td>
                <div class="" data-toggle="tooltip" data-placement="top">
                  <input name="pinyinma[]" type="text" class="form-control" placeholder="拼音码" autocomplete="off">
                  <input name="stock_id[]" type="hidden" class="form-control">
                </div>
              </td>
              <td>
                <div class="">
                  <input name="name[]" type="text" class="form-control" readonly placeholder="药品名称" tabindex="-1">
                </div>
              </td>
              <td>
                <input name="spec[]" type="text" class="form-control" readonly placeholder="规格" tabindex="-1">
              </td>
              <td>
                <input name="unit[]" type="text" class="form-control" readonly placeholder="单位" tabindex="-1">
              </td>
              <td>
                <input name="price[]" type="text" class="form-control" readonly placeholder="0.00" tabindex="-1">
              </td>
              <td>
                <input name="amount[]" type="number" class="form-control" placeholder="0">
              </td>
              <td>
                <input name="subtotal[]" type="number" class="form-control" readonly placeholder="自动计算..." tabindex="-1">
              </td>
            </tr>
            <tr>
              <td>
                <div class="" data-toggle="tooltip" data-placement="top">
                  <input name="pinyinma[]" type="text" class="form-control" placeholder="拼音码" autocomplete="off">
                  <input name="stock_id[]" type="hidden" class="form-control">
                </div>
              </td>
              <td>
                <div class="">
                  <input name="name[]" type="text" class="form-control" readonly placeholder="药品名称" tabindex="-1">
                </div>
              </td>
              <td>
                <input name="spec[]" type="text" class="form-control" readonly placeholder="规格" tabindex="-1">
              </td>
              <td>
                <input name="unit[]" type="text" class="form-control" readonly placeholder="单位" tabindex="-1">
              </td>
              <td>
                <input name="price[]" type="text" class="form-control" readonly placeholder="0.00" tabindex="-1">
              </td>
              <td>
                <input name="amount[]" type="number" class="form-control" placeholder="0">
              </td>
              <td>
                <input name="subtotal[]" type="number" class="form-control" readonly placeholder="自动计算..." tabindex="-1">
              </td>
            </tr>
            </tbody>
          </table>
          <div class="col-sm-10">
            总计:<span id="totalPrice">0.0</span>元
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button id="doSell" type="button" class="btn btn-success submit">确认销售</button>
              <button type="reset" class="btn btn-warning">清空输入</button>
            </div>
          </div>
        </form>
      </div>
    </div>
    <div class="panel panel-default func" id="sellList">
      <div class="panel-heading" style="text-align: center">销售记录</div>
      <table class="table table-hover">
        <thead>
        <tr>
          <th>销售码</th>
          <th>订单编号</th>
          <th>药品名称</th>
          <th>规格</th>
          <th>单位</th>
          <th>单价</th>
          <th>数量</th>
          <th>总额</th>
          <th>销售日期</th>
          <th>操作员</th>
          <th>状态</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="info in list">
          <td>{{ info.sell_id }}</td>
          <td>{{ info.orderno }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.spec }}</td>
          <td>{{ info.unit }}</td>
          <td>{{ info.price }}</td>
          <td>{{ info.sell_amount }}</td>
          <td>{{ info.subtotal }}</td>
          <td>{{ info.time }}</td>
          <td>{{ info.realname }}</td>
          <td v-if="info.sell_status == 1">退货</td>
          <td v-else>正常</td>
        </tr>
        </tbody>
      </table>
      {$show}
      <div id="sellPage" class="center">
        <nav>
          <ul class="pagination">
            <li v-for="i in count">
              <a href="javascript:" :data-to="i">{{ i }}</a>
            </li>
          </ul>
        </nav>
      </div>
    </div>
  </div>
</div>
<iframe id="ifm" style="display: none"></iframe>
<include file="./Public/Tpl/footer.html"/>
<js href="__PUBLIC__/js/jquery.Kcomplete-1.1.js"/>
<script>
    var size = 10;
    var page = 1;
    var sellList = {list: [], count: 10};
    var app = new Vue({
        el: '#sellList',
        data: sellList
    });
    function loadList(size, page) {
        $.ajax({
            type: 'POST',
            url: "{:U('Index/getSellList')}",
            data: {"do": "loadmore", "size": size, "page": page},
            dataType: 'json',
            async:false,
            success: function (rs) {
                if (rs.code == 200) {
                    sellList.list = rs.data.list;
                    sellList.count = Math.ceil(rs.data.count/size);
                }
            },
            beforeSend:function(){
                this.layerIndex = layer.load(1,{ shade: [0.1, '#393D49'] });
            },
            complete:function(){
                layer.close(this.layerIndex);
            }
        });
    }

    $(document).ready(function () {
        //加载更多
        layui.use(['laypage', 'layer'], function () {
            loadList(size,page);
            layer = layui.layer;
            laypage = layui.laypage;
            laypage({
                cont: 'sellPage'
                , pages: sellList.count
                , skin: '#337ab7'
                , skip: true
                ,jump: function (obj, first) {
                    if (!first) {
                        page = obj.curr;
                        loadList(size, page);
                    }
                }
            });
        });
        //拼音码自动提示
        $("input[name='pinyinma[]']").each(function () {
            $(this).Kcomplete({
                location: "{:U('Drug/nameTips')}",
                dataType: 'json'
            });
        });
        //提交事件
        $("#doSell").on("click", function () {
            $.ajax({
                url: "{:U('Sell/doAdd')}",
                data: $("#sellForm").serialize(),
                datatype: "json",
                type: "POST",
                beforeSend: function () {
                    this.layerIndex = layer.load(1,{ shade: [0.1, '#393D49'] });
                },
                success: function (data) {
                    console.log(data);
                    if (data['code'] == 200) {
                        layer.msg("销售成功!",{icon:1});
                        /*layer.open({
                            type: 2,
                            title: '打印订单',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['380px', '90%'],
                            content: data.data
                        });*/
                        $("#ifm").attr('src', data.data);
                        $("#sellForm")[0].reset();
//                        window.location.reload();
                    } else {
                        layer.msg(data.msg,{icon:2});
                    }
                },
                error: function () {
                    layer.msg("提交过程中发生问题! 请联系管理员",{icon:0});
                },
                complete:function(){
                    layer.close(this.layerIndex);
                }
            })
        });
        //自动算价
        $("input[name='amount[]']").on("keyup", function () {
            $Price = $(this).parents('tr').find("[name='price[]']");
            $allPrice = $(this).parents('tr').find("[name='subtotal[]']");
            if (this.value !== "" && $Price.val() != "") {
                var subTotal = (this.value * $Price.val()).toFixed(2);
                $allPrice.attr("value", subTotal);
            }
            $("#totalPrice").html("0");
            for (var i = 0; i < $("[name='subtotal[]']").length; i++) {
                var oldPrice = parseFloat($("#totalPrice").html());
                var add = parseFloat($("[name='subtotal[]']").eq(i).val());
                if (add)
                    $("#totalPrice").html((oldPrice + add).toFixed(2));
            }
        });
        //拼音码事件
        $("input[name='pinyinma[]']").bind("keyup change", function () {
            if (this.value == "") {
                return;
            }
            $pTr = $(this).parents('tr');
            $pTr.find("[name='subtotal[]']").attr("value", "");
            $.ajax({
                url: "{:U('Stock/getInfo')}",
                data: "pinyinma=" + $(this).val(),
                datatype: "json",
                type: "POST",
                success: function (data) {
                    console.log(data);
                    if (data['code'] == 200) {
                        var tempStockId = data.data.stock_id;
                        var tempName = data.data.name;
                        var tempSpec = data.data.spec;
                        var tempUnit = data.data.unit;
                        var tempSellprice = data.data.sellprice;
                    } else {
                        var tempName = data.msg;
                        var tempSpec = data.msg;
                        var tempUnit = data.msg;
                        var tempSellprice = data.msg;
                    }
                    $pTr.find("[name='stock_id[]']").attr("value", tempStockId);
                    $pTr.find("[name='name[]']").attr("value", tempName);
                    $pTr.find("[name='spec[]']").attr("value", tempSpec);
                    $pTr.find("[name='unit[]']").attr("value", tempUnit);
                    $pTr.find("[name='price[]']").attr("value", tempSellprice);
                },
                error: function () {
                },
            })
        });

    });
</script>
</body>
</html>